import axios from 'axios'
export default {
  namespaced: true,
  state () {
    return {
      list: []
    }
  },
  mutations: {
    getData (state, list) {
      state.list = list
    },
    changeC (state, obj) {
      const res = state.list.find(item => item.id === obj.id)
      res.count = obj.count
    }
  },
  actions: {
    async getList (context) {
      const res = await axios.get('http://localhost:3000/cart')
      console.log(res)
      context.commit('getData', res.data)
    },
    async changeCount (context, obj) {
      await axios.patch('http://localhost:3000/cart/' + obj.id, { count: Object.count })

      context.commit('changeC', obj)
    }
  },
  getters: {
    totalCount (state) {
      return state.list.reduce((sum, item) => sum + item.count, 0)
    },
    totalPrice (state) {
      return state.list.reduce((sum, item) => sum + item.count * item.price, 0)
    }
  }

}
